package com.sunlee.admin.api.module.sys.controller;

import com.sunlee.admin.api.annotation.LogOperation;
import com.sunlee.admin.api.module.sys.dto.SysDictTypeDTO;
import com.sunlee.admin.api.module.sys.entity.DictType;
import com.sunlee.admin.api.module.sys.service.SysDictTypeService;
import com.sunlee.common.core.constant.Constant;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.entity.Result;
import com.sunlee.common.core.utils.AssertUtils;
import com.sunlee.common.core.validator.group.DefaultGroup;
import com.sunlee.common.core.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 字典类型
 *
 * @author sunlee
 */
@RestController
@RequestMapping("sys/dict/type")
@Api(tags = "字典类型")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @GetMapping("page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dictType", value = "字典类型", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dictName", value = "字典名称", paramType = "query", dataType = "String", dataTypeClass = String.class),
    })
    @RequiresPermissions("sys:dict:page")
    public Result<PageData<SysDictTypeDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        
        PageData<SysDictTypeDTO> page = sysDictTypeService.page(params);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("sys:dict:info")
    public Result<SysDictTypeDTO> get(@PathVariable("id") Long id) {
        
        SysDictTypeDTO data = sysDictTypeService.get(id);
        return Result.ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("sys:dict:save")
    public Result<Object> save(@Validated(DefaultGroup.class) @RequestBody SysDictTypeDTO dto) {
        
        sysDictTypeService.save(dto);
        return Result.ok();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("sys:dict:update")
    public Result<Object> update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody SysDictTypeDTO dto) {
        
        sysDictTypeService.update(dto);
        return Result.ok();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("sys:dict:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {

        AssertUtils.isArrayEmpty(ids, "id");

        sysDictTypeService.delete(ids);
        return Result.ok();
    }

    @GetMapping("all")
    @ApiOperation("所有字典数据")
    public Result<List<DictType>> all() {

        List<DictType> list = sysDictTypeService.getAllList();
        return Result.ok(list);
    }

}